package com.wgw.first.app;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.wgw.first.R;
import com.wgw.first.base.BaseFragment;
import com.wgw.first.cart.fragment.CartFragment;
import com.wgw.first.home.fragment.homeFragment;
import com.wgw.first.user.fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wgw
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class MainActivity extends FragmentActivity {

    private RadioGroup mRg_main;
    private List<BaseFragment> mBaseFragment;
    private int mPosition = 0;
    private String json_cart;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置软件的主界面显示并找到mRg_main的id
        setContentView(R.layout.avtivity_main);
        mRg_main = (RadioGroup) findViewById(R.id.rg_main);


        //建立一个fragment集合,并往里面存放实例化的fragment对象
        mBaseFragment = new ArrayList<>();
        mBaseFragment.add(new homeFragment());
        mBaseFragment.add(new CartFragment());
        mBaseFragment.add(new UserFragment());


        //设置默认选中主页面按钮
        mRg_main.check(R.id.rb_home);
        //刷新主页面fragment
        BaseFragment to = mBaseFragment.get(0);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.ly_content, to);
        transaction.commit();


        //用position来设置用户点击的是第几个按钮
        //设置RadioGroup的监听，根据监听替换fragment
        mRg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        mPosition = 0;
                        replaceFragment();

                        break;


                    case R.id.rb_cart:
                        mPosition = 1;
                        replaceFragment();
                        break;


                    case R.id.rb_user:
                        mPosition = 2;
                        replaceFragment();
                        break;

                    default:
                        mPosition = 0;
                        replaceFragment();
                        break;
                }
            }
        });


    }

    /**
     * 替换fragment
     */
    public void replaceFragment() {

        BaseFragment to = mBaseFragment.get(mPosition);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.ly_content, to);
        transaction.commit();


    }

}
